import 'dart:convert';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dms_file_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/dms_file.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/loading_dialog.dart';

class DmsUserqualificationsFileUpload extends StatefulWidget {
  String? documentno;

  DmsUserqualificationsFileUpload({required this.documentno});

  @override
  _DmsUserqualificationsFileUploadState createState() =>
      _DmsUserqualificationsFileUploadState();
}

class _DmsUserqualificationsFileUploadState
    extends State<DmsUserqualificationsFileUpload> {
  late DmsFile dmsFile = DmsFile();

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  List<SelectOptionVO> documentTypeList = [];

  @override
  void initState() {
    super.initState();
    dmsFile.documentno = widget.documentno;
  }

  _upload() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom, allowedExtensions: ['pdf'], withData: true);

    if (result == null) {
      return;
    }
    // if (result.files.first.size > 1048 * 1048 * 20) {
    //   cryAlert(context, S.of(context).sizeLimit);
    //   return;
    // }
    formKey.currentState!.save();

    setState(() {
      dmsFile.content = base64Encode(result.files.first.bytes!);
      dmsFile.name = result.files.first.name;
    });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '附件名称',
            value: dmsFile.name,
            enable: false,
            onSaved: (v) {
              dmsFile.name = v;
            },
            validator: (v) {
              return v!.isEmpty ? "请上传附件" : null;
            },
          ),
          // Center(child: Text("上传文件大小不超过20M",style: TextStyle(color: Colors.red),),),
          CryInput(
            label: '备注',
            value: dmsFile.remark,
            onSaved: (v) {
              dmsFile.remark = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              CryUtils.message("请上传附件");
              return;
            }
            //打开遮罩
            showDialog(
                context: context,
                builder: (BuildContext context) {
                  return LoadingDialog(
                    title: "上传中...",
                    isCancel: false,
                  );
                });
            form.save();
            dmsFile.directoryId = "634";
            DmsFileOdooApi.upload(dmsFile.toOdooMap())
                .then((ResponseBodyApi res) {
              //关闭遮罩
              Navigator.pop(context, true);
              if (!res.success!) {
                // CryUtils.message(res.message!);
                return;
              }
              //关闭该窗口
              Navigator.pop(context, true);
              CryUtils.message("上传成功");
            }).onError((error, stackTrace) {
              //关闭遮罩
              Navigator.pop(context, true);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("文档上传"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            form,
            CryButton(
              label: "上传附件",
              onPressed: () {
                _upload();
              },
              iconData: Icons.upload_rounded,
            )
          ],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
